//
//  ViewController.m
//  Personinformation
//
//  Created by Gamefire on 16/4/19.
//  Copyright © 2016年 Gamefire. All rights reserved.
//

#import "PersonViewController.h"
#import "EditierViewController.h"
#import "UserInfoModel.h"
#import "YearPickVIew.h"
#import "Header.h"

#import "WULIChangeUserInfoAPI.h"
#import "UIImageView+WebCache.h"

@interface PersonViewController ()<ageDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{

    UILabel *manlabel;
    UILabel *wmanlabel;
    UIButton *wmanbtn;
    UIButton *manbtn;
    EditierViewController *editier;
    UILabel *opinionname;
    UIButton *exitbtn;
    YearPickVIew *yearpickView;
    
    NSString *ageString;
    
    UIImageView *topimageView;
}

@end

@implementation PersonViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    
    
    [self.view setBackgroundColor:[UIColor colorWithRed:236.0/255.0 green:236.0/255.0 blue:236.0/255.0 alpha:1]];
    
    UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    [navigationView setBackgroundColor:[UIColor colorWithRed:242.0 /255.0 green:202.0 /255.0 blue:48.0/255.0 alpha:1]];
    [self.view addSubview:navigationView];
    
    UIImageView *leftimageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 27, 10, 20)];
    [leftimageView setImage:[UIImage imageNamed:@"Detalback"]];
    [navigationView addSubview:leftimageView];
    
    UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(navigationView.bounds.size.width / 2.0 - 33.5, 28, 67, 20)];
    [namelabel setText:@"个人中心"];
    [namelabel setFont:[UIFont systemFontOfSize:16.0]];
    [navigationView addSubview:namelabel];
    
    UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(23, 26, 33, 24)];
    [backLabel setTextColor:[UIColor blackColor]];
    [backLabel setText:@"返回"];
    [backLabel setTextAlignment:NSTextAlignmentCenter];
    [backLabel setFont:[UIFont systemFontOfSize:15.0]];
    [navigationView addSubview:backLabel];
    
    UIButton *leftbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 65)];
    [leftbtn addTarget:self action:@selector(leftEvent:) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:leftbtn];
    // 头像
    UIButton *cachebtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(navigationView.frame), [UIScreen mainScreen].bounds.size.width, 49)];
    [cachebtn setBackgroundColor:[UIColor whiteColor]];
    [cachebtn addTarget:self action:@selector(topimageEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cachebtn];
    //昵称
    UIButton *opinionbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(cachebtn.frame), [UIScreen mainScreen].bounds.size.width, 49)];
    [opinionbtn setBackgroundColor:[UIColor whiteColor]];
    [opinionbtn addTarget:self action:@selector(nickname:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:opinionbtn];
    //性别
    UIButton *aboutbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(opinionbtn.frame), [UIScreen mainScreen].bounds.size.width, 49)];
    [aboutbtn setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:aboutbtn];
    // 年龄
    exitbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(aboutbtn.frame), [UIScreen mainScreen].bounds.size.width, 49)];
    [exitbtn setBackgroundColor:[UIColor whiteColor]];
    [exitbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [exitbtn addTarget:self action:@selector(yearEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:exitbtn];
    
    UILabel *cachelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 60, 15)];
    [cachelabel setBackgroundColor:[UIColor whiteColor]];
    [cachelabel setText:@"头像"];
    [cachelabel setFont:[UIFont systemFontOfSize:15.0]];
    [cachebtn addSubview:cachelabel];
    
    UILabel *opinionlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 60, 15)];
    [opinionlabel setBackgroundColor:[UIColor whiteColor]];
    [opinionlabel setText:@"昵称"];
    [opinionlabel setFont:[UIFont systemFontOfSize:15.0]];
    [opinionbtn addSubview:opinionlabel];
    
    UILabel *aboutlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 60, 15)];
    [aboutlabel setText:@"性别"];
    [aboutlabel setFont:[UIFont systemFontOfSize:15.0]];
    [aboutlabel setBackgroundColor:[UIColor whiteColor]];
    [aboutbtn addSubview:aboutlabel];
    
    UILabel *exitlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 60, 15)];
    [exitlabel setText:@"年龄"];
    [exitlabel setFont:[UIFont systemFontOfSize:15.0]];
    [exitlabel setBackgroundColor:[UIColor whiteColor]];
    [exitbtn addSubview:exitlabel];
    UIImage *image = nil;
    image = [UIImage imageNamed:@"ArrowRight"];
    UIImageView *cacheimageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20, 15,10,20)];
//    [cacheimageView setBackgroundColor:[UIColor blueColor]];
    
    [cacheimageView setImage:image];
    [cachebtn addSubview:cacheimageView];
    
    UIImageView *opinionimageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20, 15,10,20)];
    [opinionimageView setImage:image];
    [opinionbtn addSubview:opinionimageView];
    
    UIImageView *exitimageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20, 15,10,20)];
    [exitimageView setImage:image];
    [exitbtn addSubview:exitimageView];
    
    UIView *cacheView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, [UIScreen mainScreen].bounds.size.width, 1.5)];
    [cacheView setBackgroundColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1]];
    [cachebtn addSubview:cacheView];
    
    UIView *opinionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, [UIScreen mainScreen].bounds.size.width, 1.5)];
    [opinionView setBackgroundColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1]];
    [opinionbtn addSubview:opinionView];
    
    UIView *aboutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, [UIScreen mainScreen].bounds.size.width, 1.5)];
    [aboutView setBackgroundColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1]];
    [aboutbtn addSubview:aboutView];
    
    UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, [UIScreen mainScreen].bounds.size.width, 1.5)];
    [exitView setBackgroundColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1]];
    [exitbtn addSubview:exitView];
    
    topimageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 70, 7, 35, 35)];
//    [topimageView setBackgroundColor:[UIColor redColor]];
    [topimageView setImage:[UIImage imageWithData:[UserInfoModel imageHeadData]]];
    [topimageView.layer setMasksToBounds:YES];
    [topimageView.layer setCornerRadius:35.0 /2.0];
    [cachebtn addSubview:topimageView];
    
    opinionname = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 75, 15, 49, 19)];
    editier = [[EditierViewController alloc] init];
    [opinionname setText:[UserInfoModel nameString]];
    [opinionname setTextAlignment:NSTextAlignmentLeft];
    [opinionname setFont:[UIFont systemFontOfSize:14.0]];
    [opinionname setTextColor:[UIColor colorWithRed:124.0/255.0 green:123.0/ 255.0 blue:124.0/ 255.0 alpha:1]];
    [opinionbtn addSubview:opinionname];
    
    
    _yearlabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 50, 16, 25, 20)];
    [_yearlabel setTextColor:[UIColor colorWithRed:124.0/255.0 green:123.0/255.0 blue:124.0/255.0 alpha:1]];
    [_yearlabel setText:[UserInfoModel UserAge]];
    [_yearlabel setFont:[UIFont systemFontOfSize:14.0]];
    [exitbtn addSubview:_yearlabel];
    
    
    manbtn = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 134, 12.0, 24.0, 24.0)];
    [manbtn setImage:[UIImage imageNamed:@"backgroundround"] forState:UIControlStateNormal];
    [manbtn addTarget:self action:@selector(ManTouchEvent) forControlEvents:UIControlEventTouchUpInside];
    [aboutbtn addSubview:manbtn];
    
    manlabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 105, 13.0, 20, 20)];
    [manlabel setText:@"男"];
    [manlabel setFont:[UIFont systemFontOfSize:12.0]];
    [manlabel setTextColor:[UIColor colorWithRed:110.0/255.0 green:110.0/ 255.0 blue:110.0/255.0 alpha:1]];
    [aboutbtn addSubview:manlabel];
    
    wmanbtn = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 66, 12.0, 24.0, 24.0)];
    [wmanbtn.layer setCornerRadius:12.0];
    [wmanbtn setImage:[UIImage imageNamed:@"backgroundround"] forState:UIControlStateNormal];
    [aboutbtn addSubview:wmanbtn];
    
    wmanlabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 37, 13.0, 20, 20)];
    [wmanlabel setText:@"女"];
    [wmanlabel setFont:[UIFont systemFontOfSize:12.0]];
    [wmanlabel setTextColor:[UIColor colorWithRed:110.0/255.0 green:110.0/ 255.0 blue:110.0/255.0 alpha:1]];
    [wmanbtn addTarget:self action:@selector(WmanTouchEvent) forControlEvents:UIControlEventTouchUpInside];
    [aboutbtn addSubview:wmanlabel];
    NSLog(@"SEX ===== %@",[UserInfoModel SexString]);
    if ([[UserInfoModel SexString]isEqual: @"0"]) {
        
        [wmanbtn setSelected:YES];
        [wmanbtn setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
    
    }else{
        
        [manbtn setSelected:YES];
        [manbtn setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
    
    }
    yearpickView = [[YearPickVIew alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 200)];
    [yearpickView setDelegate:self];
    [yearpickView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:yearpickView];

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userChangeInfoGetBack:) name:@"xiugaiuser" object:nil];
}

- (void)ManTouchEvent{
    
    manbtn.tag = 1;
    
    [manbtn setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
    [wmanbtn setImage:[UIImage imageNamed:@"backgroundround"] forState:UIControlStateNormal];
    [manlabel setTextColor:[UIColor blackColor]];
    [wmanlabel setTextColor:[UIColor colorWithRed:110.0/255.0 green:110.0/ 255.0 blue:110.0/255.0 alpha:1]];
    
}



- (void)WmanTouchEvent{
    wmanbtn.tag = 0;
    
    [manbtn setImage:[UIImage imageNamed:@"backgroundround"] forState:UIControlStateNormal];
    [wmanbtn setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
    [manlabel setTextColor:[UIColor colorWithRed:110.0/255.0 green:110.0/ 255.0 blue:110.0/255.0 alpha:1]];
    [wmanlabel setTextColor:[UIColor blackColor]];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController setHidesBottomBarWhenPushed:YES];
//    [opinionname setText:editier.textfiled.text];
}



// 头像
- (void)topimageEvent:(UIButton *)sender{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"从相册中选取", @"拍照", nil];
    
    // Show the sheet
    [sheet showInView:self.view];
}
//昵称
- (void)nickname:(UIButton *)sender{

    editier.tString = [UserInfoModel nameString];
    [self.navigationController pushViewController:editier animated:YES];

}
// 年龄
- (void)yearEvent:(UIButton *)sender{

    UIButton *button = exitbtn;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame;
        frame = yearpickView.frame;
//        CGFloat delta = frame.origin.y;
        frame.origin.y = 400;
        yearpickView.frame = frame;
    }];

    if (exitbtn.selected == YES) {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame;
        frame = yearpickView.frame;
//        CGFloat delta = frame.origin.y;
        frame.origin.y = heightView;
        yearpickView.frame = frame;
    }];

    }
    button.selected = !exitbtn.selected;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark----age
- (void)TouchEvent:(UIButton *)button
{
    ageString = yearpickView.age;
    [WULIChangeUserInfoAPI putUserInformation:@{@"age":yearpickView.age}];
}



//返回
- (void)leftEvent:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark-----xhangeinfo
- (void)userChangeInfoGetBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        [_yearlabel setText:ageString];
    }
    else
    {
        [_yearlabel setText:[UserInfoModel UserAge]];
        
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alterView show];
        
    }
}





@end
